package com.asen.libcommon.widget

import android.content.Context
import android.view.View
import com.asen.libcommon.R
import com.asen.libcommon.base.viewbind.bindView
import com.asen.libcommon.databinding.XpopupCommonPupHintBinding
import com.lxj.xpopup.core.CenterPopupView

/**
 * @author Lin
 * @time 2021/3/9 15:04
 *
 * @desc 提示用弹窗
 */

class CommonHintWindow(context: Context) : CenterPopupView(context) {

    override fun getImplLayoutId() = R.layout.xpopup_common_pup_hint

    private val mBinding: XpopupCommonPupHintBinding by bindView()

    private var title: String? = null   //标题
    private var context: String? = null //内容
    private var cancelText: String? = null //内容
    private var ensureText: String = "确定" //内容
    private var cancel: (() -> Unit)? = null  //取消
    private var ensure: (() -> Unit)? = null  //确认

    // 是否点击了取消按钮
    private var isClickCancel = false

    override fun onCreate() {
        super.onCreate()
        //标题
        mBinding.tvTitle.text = title
        mBinding.tvTitle.visibility = if (title.isNullOrEmpty()) View.GONE else View.VISIBLE
        //内容
        mBinding.tvContent.text = context
        mBinding.tvContent.visibility = if (context.isNullOrEmpty()) View.GONE else View.VISIBLE
        //取消
        if (!cancelText.isNullOrEmpty()) {
            mBinding.vDivider.visibility = View.VISIBLE
            mBinding.btnCancel.visibility = View.VISIBLE
            mBinding.btnCancel.text = cancelText
        }
        //确定按钮
        mBinding.btnEnsure.text = ensureText
        mBinding.btnCancel.setOnClickListener {
            if (isShow) {
                dismiss()
                isClickCancel = true
            }
        }
        mBinding.btnEnsure.setOnClickListener {
            if (isShow) {
                dismiss()
            }
            ensure?.invoke()
        }
    }

    /**
     * 设置标题
     */
    fun title(title: String?): CommonHintWindow {
        this.title = title
        return this
    }

    /**
     * 设置内容
     */
    fun content(content: String?): CommonHintWindow {
        this.context = content
        return this
    }

    /**
     * 设置取消事件
     */
    fun cancel(text: String? = "取消", cancel: (() -> Unit)?=null): CommonHintWindow {
        this.cancelText = text
        this.cancel = cancel
        return this
    }

    /**
     * 确认事件
     */
    fun ensure(text: String = "确定", ensure:  (() -> Unit)?=null): CommonHintWindow {
        this.ensureText = text
        this.ensure = ensure
        return this
    }

    override fun onDismiss() {
        super.onDismiss()
        if (isClickCancel)cancel?.invoke()
    }
}